package character;

import java.util.ArrayDeque;
import java.util.Deque;
import action.Action;
import game.DataStore;
import action.MoveAction;
import board.Tile;
import com.jme3.math.FastMath;
import misc.Vector3i;

public class ActionQueue {
    protected Character character;
    protected Deque<Action> actions = new ArrayDeque<Action>();
    protected Deque<Tile> movementPath = new ArrayDeque<Tile>();
    protected Vector3i queuedLocation;
    protected int ticksQueued = 0;
    
    @Deprecated
    public ActionQueue() {}
    
    public ActionQueue(Character character) {
        this.character = character;
    }
    
    public Deque<Action> getActions() {
        return actions;
    }

    public void queueActionLast(Action action) {
        actions.addLast(action);
	ticksQueued += action.getTotalTicks();
        if (action instanceof MoveAction) {
            queuedLocation = ((MoveAction)action).getDestination();
            queueToMovementPathLast(DataStore.board.getTile(((MoveAction) action).getDestination()));
        }
    }
    public void dequeueAction(Action action) {
        actions.removeFirstOccurrence(action);
	ticksQueued -= action.getTicksRemaining();
    }
    public void clearAllActions() {
        actions.clear();
        clearMovementPath();
	ticksQueued = 0;
    }

    protected void queueToMovementPathLast(Tile tile) {
        movementPath.addLast(tile);
        DataStore.board.addMovementPath(tile, character);
    }
    public void dequeueFromMovementPath(Tile tile) {
        movementPath.removeFirstOccurrence(tile);
        if (!movementPath.contains(tile)) {
            DataStore.board.removeMovementPath(tile, character);
        }
    }

    protected void clearMovementPath() {
        for (Tile tile : movementPath) {
            DataStore.board.removeMovementPath(tile, character);
        }
        movementPath.clear();
	queuedLocation = character.getBoardLocation().clone();
    }
    
    public Vector3i getQueuedLocation() {
        return queuedLocation;
    }
    public void setQueuedLocation(Vector3i location) {
        queuedLocation = location;
    }
    public int getTicksQueued() {
	return ticksQueued;
    }
    public void resetTicksQueued() {
	ticksQueued = 0;
    }
}
